package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import apps.Events;
import apps.Info;

/**
 * Servlet implementation class CreateEvent
 */
public class CreateEvent extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Events event = null;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateEvent() {
		super();
		// TODO Auto-generated constructor stub
	}



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String[] party = request.getParameter("parties").toString().split(" ");
		Vector partie = new Vector();
		HttpSession session = request.getSession();
		;
		for (int i = 0; i < party.length; i++) {
			if (party[i].length() > 2) {
				partie.add(party[i]);
			}

		}
		Date start = new Date();
		start.setDate(Integer.parseInt(request.getParameter("startDate")));
		start.setMonth(Integer.parseInt(request.getParameter("startMonth")));
		start.setYear(Integer.parseInt(request.getParameter("startYear")));
		start.setMinutes(Integer.parseInt(request.getParameter("startMin")));
		start.setHours(Integer.parseInt(request.getParameter("startHr")));
		Date end = new Date();
		end.setDate(Integer.parseInt(request.getParameter("endDate")));
		end.setMonth(Integer.parseInt(request.getParameter("endMonth")));
		end.setYear(Integer.parseInt(request.getParameter("endYear")));
		end.setMinutes(Integer.parseInt(request.getParameter("endMin")));
		end.setHours(Integer.parseInt(request.getParameter("endHr")));
		event = new Events(start.toString(), end.toString(), request
				.getParameter("description").toString(), partie);

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<HTML><HEAD><TITLE>Form Example</TITLE></HEAD><BODY>");
		String edit = new String();
		edit = request.getParameter("edit");
		if (Info.putInfo(event) == false) {
			out.println("<p>Unable to create event</p>");
		} else {
			out.println("<p>Event Created</p>");
		}
		Enumeration<String> enums = session.getAttributeNames();
		/*while (enums.hasMoreElements()) {
			
			System.out.println("1:"+session.getAttribute(enums.nextElement()));
		}*/
		
		if (false) {
			System.out.println(edit);
			Vector events = new Vector();
			events =(Vector)session.getAttribute("info.value");
			int when = Integer.parseInt(session.getAttribute("when").toString());
			Info.delInfo((Events)events.elementAt(when));
		}
		out.println("<p>Start: " + event.getStart() + "<br>Description: "
				+ event.getDesc() + "<br>End: " + event.getEnd()
				+ "<br>Invited: ");
		for (int i = 0; i < event.getParties().size(); i++) {
			out.println("<br>" + event.getParties().elementAt(i));
		}
		out.println("</p>" + "<a href=\"LoginHandler\" >Back to Calendar</a>");
		out.println("</BODY></HTML>");
		out.close();
	}

}
